/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.action;

import java.util.ArrayList;
import java.util.regex.Pattern;

import com.bluestone.context.IContext;
import com.bluestone.scripts.ActionScript;
import com.bluestone.util.Util;

/**
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 * 
 */
public class CheckFalseAction extends CheckAction {
	public CheckFalseAction(ActionScript action){
		super(action);
	}



	/**
	 * Check the pageContent whether contain the values.
	 * @return true if the pageContents does not contain any of the values.
	 */
	public boolean execute(IContext context) {

		if (!super.execute(context)){
			return false;
		}
		String value = action.getPara("value");
		if(value.startsWith(REGEXPATTERN)){
			action.setPara("value", value.substring(REGEXPATTERN.length()));
			if(!doRegexPattern()){
				return false;
			}
		}else if(value.startsWith(EXACTPATTERN)){
			action.setPara("value", value.substring(EXACTPATTERN.length()));
			if(!doExactPattern()){
				return false;
			}			
		} else{
			if(!doExactPattern()){
				return false;
			}
		}
		String target = action.getPara("target");
		if (target == null || target.length() == 0)
			return true;
		String delay = action.getPara("delay");
		if (delay != null) {
			robot.delay(Util.getDelayTime(Integer.parseInt(delay)));
		} else {
			robot.delay(Util.getDelayTime(Util.CHECKDELAY));
		}
		return true;
	}
	
	private boolean doRegexPattern(){
		ArrayList list = split(action.getPara("value"));
		for(int i=0; i<list.size(); i++){
			String temp = (String)list.get(i);
			if(Pattern.matches(temp, pageContent)){
				Util.warn("find '"+temp +"' in "+pageContent);
				return false;
			}		
		}		
		return true;
	}
	private boolean doExactPattern(){
		ArrayList list = split(action.getPara("value"));
		for(int i=0; i<list.size(); i++){
			String temp = (String)list.get(i);
			if(pageContent.indexOf(temp)>0){
				Util.warn("find '"+temp +"' in "+pageContent);
				return false;
			}		
		}		
		return true;
	}
	private ArrayList split(String str){
		ArrayList list = new ArrayList();
		String [] values = str.split(this.spitRegex);
		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				String temp = values[i].trim();
				if(temp.startsWith("@")){
					temp = Util.getString(temp);
					list.addAll(split(temp));
				}else{
					list.add(temp);
				}
			}
		}
		return list;
	}
}
